package windows

import (
	"git-hawk/internal/logic/models"
	"git-hawk/internal/logic/services"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

// 添加窗口
func ShowAddWindow(app fyne.App, tabInfo services.TreeTabInfo, refresh func()) func() {
	return func() {
		w := app.NewWindow("添加")

		w.SetFixedSize(true) // 设置固定大小，避免用户拖动改变大小

		// w.Minimizable = false // 去掉最小化按钮
		// w.SetMaximizable(false)

		name := widget.NewLabel("名称:  ")
		nameInput := widget.NewEntry()
		nameInput.SetPlaceHolder("请输入名称")
		//多行输入
		nameInput.MultiLine = true
		nameInput.Wrapping = 2 // 开启自动换行
		nameRow := container.NewBorder(nil, nil, name, nil, nameInput)

		username := widget.NewLabel("用户名:")
		usernameText := widget.NewEntry()
		usernameText.SetPlaceHolder("请输入用户名")
		//多行输入
		usernameText.MultiLine = true
		usernameText.Wrapping = 2 // 开启自动换行
		usernameRow := container.NewBorder(nil, nil, username, nil, usernameText)

		password := widget.NewLabel("密码:  ")
		passwordInput := widget.NewEntry()
		passwordInput.SetPlaceHolder("请输入密码")
		//多行输入
		passwordInput.MultiLine = true
		passwordInput.Wrapping = 2 // 开启自动换行
		passwordLayout := container.NewBorder(nil, nil, password, nil, passwordInput)

		desc := widget.NewLabel("描述:  ")
		descInput := widget.NewEntry()
		descInput.SetPlaceHolder("请输入描述")
		//多行输入
		descInput.MultiLine = true
		descInput.Wrapping = 2 // 开启自动换行
		descLayout := container.NewBorder(nil, nil, desc, nil, descInput)

		// 创建垂直布局容器
		content := container.NewVBox()
		// 创建存储所有输入框的切片
		var allInput = make(map[string]*widget.Entry)

		addInputLabel := widget.NewLabel("输入框:")
		selectInputBox := widget.NewSelect([]string{"appid", "url"}, func(val string) {
			customLabelText := widget.NewLabel(val + ":  ")
			customInput := widget.NewEntry()
			customInput.SetPlaceHolder("请输入" + val)
			//多行输入
			customInput.MultiLine = true
			customInput.Wrapping = 2 // 开启自动换行
			// allInput = append(allInput, customInput)
			allInput[val] = customInput
			customLayout := container.NewBorder(nil, nil, customLabelText, nil, customInput)
			content.Add(customLayout)
			content.Refresh() // 刷新容器以显示新添加的组件
		})
		selectInputBox.PlaceHolder = "添加自定义输入框选项"
		addInputLayout := container.NewBorder(nil, nil, addInputLabel, nil, selectInputBox)

		saveBtn := widget.NewButton(" 保存 ", func() {
			if nameInput.Text == "" {
				toast("请输入名称", w)
				return
			}
			pass := new(models.Password)
			pass.Category_id = tabInfo.Info.Id
			pass.Name = nameInput.Text
			pass.Username = usernameText.Text
			pass.Password = passwordInput.Text
			pass.Desc = descInput.Text

			customInputType := make([]string, len(allInput))
			CustomContent := make(map[string]string, len(allInput))
			for i, entry := range allInput {
				customInputType = append(customInputType, i)
				CustomContent[i] = entry.Text
			}
			pass.CustomInputType = strings.Join(customInputType, ", ") // 用逗号和空格连接
			pass.CustomContent = &CustomContent

			// repositories.SavePassword(pass)
			refresh()
			w.Close()
		})

		cancelBtn := widget.NewButton(" 取消 ", func() {
			w.Close()
		})

		/* saveBtn.OnTapped = func() {

		} */

		/* saveBtnLayout := container.NewBorder(nil, nil, nil, container.New(layout.NewHBoxLayout(), cancelBtn, saveBtn), nil)

		buttonnLayout := container.NewBorder(nil, nil, nil, nil, saveBtnLayout)
		bodyLayout := container.NewBorder(nameRow, nil, nil, nil, usernameRow)
		addDataLayout := container.New(layout.NewVBoxLayout(), bodyLayout, passwordLayout, descLayout, addInputLayout, content, buttonnLayout)
		w.SetContent(container.NewVScroll(addDataLayout)) */

		bodyLayout := container.New(layout.NewVBoxLayout(), nameRow, usernameRow, passwordLayout, descLayout, addInputLayout, content)
		//保存按钮
		saveBtnLayout := container.NewBorder(nil, nil, nil, container.New(layout.NewHBoxLayout(), cancelBtn, saveBtn), nil)
		allLayout := container.New(layout.NewVBoxLayout(), container.NewVScroll(bodyLayout), saveBtnLayout)
		w.SetContent(allLayout)

		// start 监听主窗口的关闭事件
		// 前提是必须设置主窗口
		w.SetOnClosed(func() {
			w.Close()
		})
		// edn 监听主窗口的关闭事件

		// 设置窗口大小,
		w.Resize(fyne.NewSize(400, 360))
		w.CenterOnScreen()
		w.Show()
		// 确保子窗口显示在主窗口上方
		w.RequestFocus()
	}
}

// 编辑窗口
func ShowEditWindow(tabInfo models.Password, refresh func()) func() {
	return func() {
		w := fyne.CurrentApp().NewWindow("编辑")
		w.SetFixedSize(true) // 设置固定大小，避免用户拖动改变大小

		// w.Minimizable = false // 去掉最小化按钮
		// w.SetMaximizable(false)

		name := widget.NewLabel("名称:  ")
		nameInput := widget.NewEntry()
		nameInput.SetPlaceHolder("请输入名称")
		//多行输入
		nameInput.MultiLine = true
		nameInput.Wrapping = 2 // 开启自动换行
		nameInput.SetText(tabInfo.Name)
		nameRow := container.NewBorder(nil, nil, name, nil, nameInput)

		username := widget.NewLabel("用户名:")
		usernameText := widget.NewEntry()
		usernameText.SetPlaceHolder("请输入用户名")
		//多行输入
		usernameText.MultiLine = true
		usernameText.Wrapping = 2 // 开启自动换行
		usernameText.SetText(tabInfo.Username)
		usernameRow := container.NewBorder(nil, nil, username, nil, usernameText)

		password := widget.NewLabel("密码:  ")
		passwordInput := widget.NewEntry()
		passwordInput.SetPlaceHolder("请输入密码")
		//多行输入
		passwordInput.MultiLine = true
		passwordInput.Wrapping = 2 // 开启自动换行
		passwordInput.SetText(tabInfo.Password)
		passwordLayout := container.NewBorder(nil, nil, password, nil, passwordInput)

		desc := widget.NewLabel("描述:  ")
		descInput := widget.NewEntry()
		descInput.SetPlaceHolder("请输入描述")
		//多行输入
		descInput.MultiLine = true
		descInput.Wrapping = 2 // 开启自动换行
		descInput.SetText(tabInfo.Desc)
		descLayout := container.NewBorder(nil, nil, desc, nil, descInput)

		saveBtn := widget.NewButton(" 保存 ", func() {
			pass := new(models.Password)
			pass.Id = tabInfo.Id
			pass.Name = nameInput.Text
			pass.Username = usernameText.Text
			pass.Password = passwordInput.Text
			pass.Desc = descInput.Text
			// repositories.UpdatePassword(pass)
			refresh()
			w.Close()
		})

		cancelBtn := widget.NewButton(" 取消 ", func() {
			w.Close()
		})

		/* saveBtn.OnTapped = func() {

		} */

		bodyLayout := container.New(layout.NewVBoxLayout(), nameRow, usernameRow, passwordLayout, descLayout)
		//保存按钮
		saveBtnLayout := container.NewBorder(nil, nil, nil, container.New(layout.NewHBoxLayout(), cancelBtn, saveBtn), nil)
		addDataLayout := container.New(layout.NewVBoxLayout(), bodyLayout, saveBtnLayout)
		w.SetContent(addDataLayout)

		// start 监听主窗口的关闭事件
		// 前提是必须设置主窗口
		w.SetOnClosed(func() {
			w.Close()
		})
		// edn 监听主窗口的关闭事件

		// 设置窗口大小,
		w.Resize(fyne.NewSize(400, 340))
		w.CenterOnScreen()
		w.Show()
	}
}

// 提示弹窗
func toast(title string, win fyne.Window) {
	infoDialog := dialog.NewInformation("提示", title, win)

	// 显示信息对话框
	infoDialog.Show()
	/* // 使用 time.AfterFunc 在指定时间后关闭对话框
	time.AfterFunc(2*time.Second, func() {
		infoDialog.Hide()
	}) */
}
